package nostalgia.gui.forms;

import nostalgia.UserConfiguration;
import nostalgia.collection.DirectoryCollectionReader;

import javax.swing.*;
import java.awt.event.*;

public class ConfigurationDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JPasswordField lastFmPasswordField;
    private JTextField lastFmUsernameField;
    private JRadioButton localCollectionITunesRadio;
    private JRadioButton localCollectionDirectoryRadio;
    private JTextField localCollectionDirectoryField;
    private JButton localCollectionBrowseButton;
    private JRadioButton localCollectionNoneRadio;

    private boolean configurationUpdated = false;

    public ConfigurationDialog(boolean mustConfigure) {
        setTitle("Nostalgia.fm Configuration");
        setResizable(false);
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        UserConfiguration configuration = UserConfiguration.getInstsance();
        lastFmUsernameField.setText(configuration.getLastFmUsername());
        lastFmPasswordField.setText(configuration.getLastFmPassword());
        if(configuration.getLocalCollectionType().equals("none")) {
            localCollectionNoneRadio.setSelected(true);
        }
        else {
            localCollectionDirectoryRadio.setSelected(true);
        }
        onLocalCollectionRadioChange();
        localCollectionDirectoryField.setText(configuration.getLocalCollectionDirectory());

        ActionListener localCollectionRadioChangeActionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onLocalCollectionRadioChange();
            };
        };
        localCollectionITunesRadio.addActionListener(localCollectionRadioChangeActionListener);
        localCollectionDirectoryRadio.addActionListener(localCollectionRadioChangeActionListener);
        localCollectionNoneRadio.addActionListener(localCollectionRadioChangeActionListener);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        if(mustConfigure) {
            buttonCancel.setEnabled(false);
        }
        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

// call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        if(!mustConfigure) {
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    onCancel();
                }
            });
        }

// call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);


        localCollectionBrowseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCollectionBrowseButton();
            }
        });
    }

    private void onLocalCollectionRadioChange() {
        localCollectionDirectoryField.setEnabled(localCollectionDirectoryRadio.isSelected());
        localCollectionBrowseButton.setEnabled(localCollectionDirectoryRadio.isSelected());
    }

    private void onCollectionBrowseButton() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File(
                localCollectionDirectoryField.getText().trim().length() > 0 ? localCollectionDirectoryField.getText() : System.getProperty("user.home")));
        chooser.setDialogTitle("Select local music collection directory");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            localCollectionDirectoryField.setText(chooser.getSelectedFile().getAbsolutePath());
        }       
    }

    private void onOK() {
        dispose();
        try {
            UserConfiguration instance = UserConfiguration.getInstsance();
            instance.setLastFmUsername(lastFmUsernameField.getText());
            instance.setLastFmPassword(new String(lastFmPasswordField.getPassword()));
            instance.setLocalCollectionDirectory(localCollectionDirectoryField.getText());
            configurationUpdated = true;
            instance.save();
        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(this, "Could not save configuration");
            e.printStackTrace();
        }        
    }

    private void onCancel() {
        dispose();
    }

    protected boolean isConfigurationUpdated() {
        return configurationUpdated;
    }

    public static boolean showDialog(JFrame parent, boolean mustConfigure) {
        ConfigurationDialog dialog = new ConfigurationDialog(mustConfigure);
        dialog.pack();
        dialog.setLocationRelativeTo(parent);
        dialog.setVisible(true);
        return dialog.isConfigurationUpdated();
    }
}
